#include "object.hpp"

#include "misc/config.hpp"
#include "systems/res_manager.hpp"
#include "systems/logger.hpp"

#include <boost/foreach.hpp>

#include <sstream>

using namespace std;

boost::uint64_t object_t::id_counter = 0;

object_class_t::object_class_t() :
    is_resources_loaded_(false)
{
}


boost::shared_ptr<object_class_t> object_class_t::create(boost::shared_ptr<std::stringstream> &stream)
{

    config_t config(*stream);

    boost::shared_ptr<object_class_t> ret(new object_class_t);

    ret->name_ = config.tree.get<string> ("ResourceGroup");

    logger::log(TRACE) << "[Object] Loading object class " << ret->name_;

    ret->mass_ = config.tree.get<scalar_t> ("Mass");

    BOOST_FOREACH( config_t::opt_t::value_type &v, config.tree.get_child("Resources") )
                {
                    if (v.first == "Mesh")
                    {
                        ret->mesh_.push_back(v.second.data());
                    }
                    else if (v.first == "Texture")
                    {
                        ret->texture_.push_back(v.second.data());
                    }
                    else if (v.first == "Material")
                    {
                        ret->material_.push_back(v.second.data());
                    }
                    else
                    {
                        assert(!"unknown resource type");
                    }
                }

    BOOST_FOREACH( config_t::opt_t::value_type &v, config.tree.get_child("Structure") )
                {
                    ret->structure_.push_back(make_pair(v.first, v.second.data()));
                }
    return ret;
}

object_t::object_t()
{
    logger::log(DEBUG) << "[Object] Empty object instance created";
}

object_t::~object_t()
{
    logger::log(DEBUG) << "[Object] Object instance freed";
}

boost::uint64_t object_t::gen_uid()
{
    return id_counter++;
}

void object_t::move_to(vector3_t &pos)
{
    //  logger::log(DEBUG) << "[Object] Move object of class " << class_->resource_group_ << " ID = " << ID << " to " << pos.x << " " << pos.y << " " << pos.z;
    //  pos_ = pos;
}
